home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / SideScroller / MovingObject.as < prev    next >
Text File  |  2007-09-27  |  9KB  |  308 lines

  1. class SideScroller.MovingObject extends SideScroller.BasicObject
  2. {
  3.    static var BOUNCE_LOSS_HORIZONTAL = 8;
  4.    static var ACCELERATION_VERTICAL = 1.2;
  5.    static var FRICTION_HORIZONTAL = 0.4;
  6.    static var MAX_FRONT_SPEED_VERTICAL = 2;
  7.    static var MINIMUM_SPEED_VERTICAL = 0.2;
  8.    static var MINIMUM_SPEED_HORIZONTAL = 0.4;
  9.    static var ABS_MIN_SPEED = 1;
  10.    static var RETURN_RIGHT_ANGLE_ACCELERATION = 0.5;
  11.    static var DEFAULT_CLIFF_CLIMB_CAPACITY = 25;
  12.    static var ANGLE_CHANGE_DIVIDER = 1;
  13.    static var ANGLE_MINIMUM_CHANGE = 0.5;
  14.    static var MAX_ANGLE = 65;
  15.    function MovingObject(__mcRef, __oLayer)
  16.    {
  17.       super(__mcRef,__oLayer);
  18.       this.bReactToCollisions = true;
  19.       this.bShouldMove = true;
  20.       this.nSpeedX = 0;
  21.       this.nSpeedYRegistration = 0;
  22.       this.nSpeedYFront = 0;
  23.       this.nReturnRightAngleSpeed = 0;
  24.       this.nTimeSinceLastGroundHit = 0;
  25.       this.nCliffClimbCapacity = SideScroller.MovingObject.DEFAULT_CLIFF_CLIMB_CAPACITY;
  26.       this.nGroundSpeedModificator = 0;
  27.       this.oLayer.doAddListener(this);
  28.    }
  29.    function doSetRotation(__nRotation, __bRelative)
  30.    {
  31.       if(__bRelative == undefined)
  32.       {
  33.          __bRelative = false;
  34.       }
  35.       if(__bRelative)
  36.       {
  37.          this.mcRef._rotation += __nRotation;
  38.       }
  39.       else
  40.       {
  41.          this.mcRef._rotation = __nRotation;
  42.       }
  43.    }
  44.    function doMove(__nSX, __nSY, __bRelative)
  45.    {
  46.       if(__bRelative == undefined)
  47.       {
  48.          __bRelative = true;
  49.       }
  50.       if(__bRelative)
  51.       {
  52.          this.doMoveHorizontalTo(this.__get__Ref()._x + __nSX);
  53.          this.doMoveVerticalTo(this.__get__Ref()._y + __nSY);
  54.       }
  55.       else
  56.       {
  57.          this.doMoveHorizontalTo(__nSX);
  58.          this.doMoveVerticalTo(__nSY);
  59.       }
  60.    }
  61.    function doSetSpeeds(__nSX, __nSY, __bRelative)
  62.    {
  63.       if(__bRelative == undefined)
  64.       {
  65.          __bRelative = false;
  66.       }
  67.       if(__bRelative)
  68.       {
  69.          if(__nSX != undefined)
  70.          {
  71.             this.nSpeedX += __nSX;
  72.          }
  73.          if(__nSY != undefined)
  74.          {
  75.             this.nSpeedYRegistration += __nSY;
  76.          }
  77.       }
  78.       else
  79.       {
  80.          if(__nSX != undefined)
  81.          {
  82.             this.nSpeedX = __nSX;
  83.          }
  84.          if(__nSY != undefined)
  85.          {
  86.             this.nSpeedYRegistration = __nSY;
  87.          }
  88.       }
  89.    }
  90.    function doEnterFrame()
  91.    {
  92.       super.doEnterFrame();
  93.       this.doMoveBothDirections();
  94.       this.doCheckObjects();
  95.    }
  96.    function doDestroy()
  97.    {
  98.       this.oLayer.doRemoveListener(this);
  99.       super.doDestroy();
  100.    }
  101.    function get Speeds()
  102.    {
  103.       return {x:this.nSpeedX,y:this.nSpeedYRegistration};
  104.    }
  105.    function get CliffClimbCapacity()
  106.    {
  107.       return this.nCliffClimbCapacity;
  108.    }
  109.    function doMoveBothDirections()
  110.    {
  111.       this.doGroundAffectHorizontalSpeed();
  112.       var _loc2_ = this.nSpeedX + this.nGroundSpeedModificator;
  113.       if(this.bShouldMove)
  114.       {
  115.          if(_loc2_ < SideScroller.MovingObject.ABS_MIN_SPEED)
  116.          {
  117.             _loc2_ = SideScroller.MovingObject.ABS_MIN_SPEED;
  118.          }
  119.       }
  120.       this.doMoveHorizontalTo(this.__get__Ref()._x + _loc2_);
  121.       this.doMoveVerticalTo(this.__get__Ref()._y + this.nSpeedYRegistration);
  122.    }
  123.    function doMoveHorizontalTo(__nFuturePosition)
  124.    {
  125.       var _loc6_ = false;
  126.       var _loc3_ = false;
  127.       var _loc2_ = new Array();
  128.       var _loc4_ = undefined;
  129.       switch(Library.Utils.MoreMath.getPolarity(this.nSpeedX))
  130.       {
  131.          case 1:
  132.             _loc4_ = "mcHitFront";
  133.             break;
  134.          case -1:
  135.             _loc4_ = "mcHitBack";
  136.       }
  137.       for(var _loc5_ in this.mcRef.mcState)
  138.       {
  139.          if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
  140.          {
  141.             _loc2_.push(this.mcRef.mcState[_loc5_]);
  142.          }
  143.       }
  144.       for(_loc5_ in _loc2_)
  145.       {
  146.          if(!_loc3_)
  147.          {
  148.             _loc3_ = this.isHitObject(_loc2_[_loc5_]);
  149.          }
  150.       }
  151.       if(_loc3_ || _loc6_)
  152.       {
  153.          if(this.bReactToCollisions)
  154.          {
  155.             this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.BOUNCE_LOSS_HORIZONTAL);
  156.             this.doReactCollision();
  157.             this.nSpeedX *= -1;
  158.             __nFuturePosition = this.mcRef._x + this.nSpeedX;
  159.          }
  160.       }
  161.       this.mcRef._x = __nFuturePosition;
  162.    }
  163.    function doMoveVerticalTo(__nFuturePosition)
  164.    {
  165.       if(this.__get__Speeds().y < 0)
  166.       {
  167.          var _loc2_ = new Array();
  168.          var _loc4_ = "mcHitTop";
  169.          var _loc3_ = false;
  170.          for(var _loc5_ in this.mcRef.mcState)
  171.          {
  172.             if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
  173.             {
  174.                _loc2_.push(this.mcRef.mcState[_loc5_]);
  175.             }
  176.          }
  177.          for(_loc5_ in _loc2_)
  178.          {
  179.             if(!_loc3_)
  180.             {
  181.                _loc3_ = this.isHitObject(_loc2_[_loc5_]);
  182.             }
  183.          }
  184.          if(_loc3_)
  185.          {
  186.             this.nSpeedYRegistration = 0;
  187.             this.nSpeedYFront = 0;
  188.             __nFuturePosition = this.mcRef._y;
  189.          }
  190.       }
  191.       var _loc6_ = false;
  192.       var _loc8_ = this.oLayer.getFloorAt(this.__get__Ref()._x,__nFuturePosition,this);
  193.       if(Math.floor(__nFuturePosition) >= Math.floor(_loc8_) && this.nSpeedYRegistration >= 0)
  194.       {
  195.          _loc6_ = true;
  196.          this.onHitGround();
  197.          this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.FRICTION_HORIZONTAL);
  198.          __nFuturePosition = _loc8_;
  199.       }
  200.       if(!_loc6_)
  201.       {
  202.          this.nSpeedYRegistration += SideScroller.MovingObject.ACCELERATION_VERTICAL;
  203.          if(Math.abs(this.nSpeedYRegistration) < SideScroller.MovingObject.MINIMUM_SPEED_VERTICAL)
  204.          {
  205.             this.nSpeedYRegistration = 0;
  206.          }
  207.       }
  208.       else
  209.       {
  210.          this.nSpeedYRegistration = 0;
  211.       }
  212.       this.mcRef._y = __nFuturePosition;
  213.       this.doAdjustToGround(_loc6_);
  214.    }
  215.    function doAdjustToGround(__bRegistrationHitGround)
  216.    {
  217.       var _loc3_ = Library.Utils.MoreMath.getBoundsCenter(this.mcRef.mcState.mcPointRear.getBounds(this.oLayer.__get__Ref()));
  218.       var _loc7_ = this.oLayer.getFloorAt(_loc3_.x,_loc3_.y,this);
  219.       var _loc5_ = undefined;
  220.       var _loc6_ = undefined;
  221.       if(_loc7_ > _loc3_.y)
  222.       {
  223.          if(__bRegistrationHitGround)
  224.          {
  225.             this.nSpeedYFront += SideScroller.MovingObject.ACCELERATION_VERTICAL;
  226.          }
  227.          else
  228.          {
  229.             this.nSpeedYFront = 0;
  230.          }
  231.          _loc5_ = _loc3_.y + this.nSpeedYFront;
  232.          _loc6_ = false;
  233.       }
  234.       else
  235.       {
  236.          _loc5_ = _loc7_;
  237.          _loc6_ = true;
  238.          this.nSpeedYFront = 0;
  239.       }
  240.       var _loc2_ = undefined;
  241.       _loc2_ = Math.round(Library.Utils.MoreMath.getAngle(Math.round(_loc3_.x),Math.round(_loc5_),Math.round(this.mcRef._x),Math.round(this.mcRef._y)));
  242.       if(!__bRegistrationHitGround && !_loc6_)
  243.       {
  244.          this.nTimeSinceLastGroundHit = this.nTimeSinceLastGroundHit + 1;
  245.          if(this.nTimeSinceLastGroundHit > 5)
  246.          {
  247.             this.nReturnRightAngleSpeed += SideScroller.MovingObject.RETURN_RIGHT_ANGLE_ACCELERATION;
  248.             _loc2_ = Library.Utils.MoreMath.getReachZero(_loc2_,this.nReturnRightAngleSpeed);
  249.          }
  250.       }
  251.       else
  252.       {
  253.          this.nReturnRightAngleSpeed = 0;
  254.          this.nTimeSinceLastGroundHit = 0;
  255.       }
  256.       if(Math.abs(_loc2_) > SideScroller.MovingObject.MAX_ANGLE)
  257.       {
  258.          _loc2_ = Library.Utils.MoreMath.getPolarity(_loc2_) * SideScroller.MovingObject.MAX_ANGLE;
  259.       }
  260.       var _loc8_ = _loc2_ - this.mcRef._rotation;
  261.       var _loc4_ = _loc8_ / SideScroller.MovingObject.ANGLE_CHANGE_DIVIDER;
  262.       if(Math.abs(_loc4_) < SideScroller.MovingObject.ANGLE_MINIMUM_CHANGE)
  263.       {
  264.          _loc4_ = 0;
  265.       }
  266.       this.mcRef._rotation += _loc4_;
  267.    }
  268.    function doGroundAffectHorizontalSpeed()
  269.    {
  270.       var _loc2_ = 0;
  271.       switch(Library.Utils.MoreMath.getPolarity(this.mcRef._rotation))
  272.       {
  273.          case 1:
  274.             _loc2_ = 1;
  275.             break;
  276.          case -1:
  277.             _loc2_ = -1;
  278.       }
  279.       this.nGroundSpeedModificator = _loc2_ * (Math.pow(Math.abs(this.mcRef._rotation),1.2) * 0.051);
  280.    }
  281.    function isHitObject(__mcHitReference)
  282.    {
  283.       var _loc4_ = Library.Utils.MoreMath.getBoundsCenter(__mcHitReference.getBounds(this.oLayer.__get__Ref()));
  284.       var _loc3_ = this.oLayer.getCollidableObjects(this);
  285.       var _loc6_ = this.oLayer.__get__Ref()._x;
  286.       var _loc7_ = this.oLayer.__get__Ref()._y;
  287.       var _loc5_ = false;
  288.       var _loc2_ = undefined;
  289.       _loc2_ = 0;
  290.       while(_loc2_ <= _loc3_.length - 1)
  291.       {
  292.          if(_loc3_[_loc2_].Hit.hitTest(_loc4_.x + _loc6_,_loc4_.y + _loc7_,true))
  293.          {
  294.             _loc5_ = true;
  295.             _loc3_[_loc2_].onHit(this);
  296.          }
  297.          _loc2_ = _loc2_ + 1;
  298.       }
  299.       return _loc5_;
  300.    }
  301.    function onHitGround()
  302.    {
  303.    }
  304.    function doReactCollision()
  305.    {
  306.    }
  307. }
  308.